<?php

namespace app\api\controller;
use app\shop\logic\LoginLogic;
use think\facade\Log;
use app\shop\library\ShopHelper;
use app\common\controller\Frontend;
class Wechat extends Frontend
{
    public function __construct(){
        header("Content-type: text/html; charset=utf-8");
        $officialaccountConfig = ShopHelper::get_setting_group('officialaccount_config');
        $this->offAppId     = $officialaccountConfig['off_app_id'] ?? '';
        $this->offAppSecret = $officialaccountConfig['off_secret'] ?? '';
        $this->offEncodingAESKey = $officialaccountConfig['EncodingAESKey'] ?? '';
        $this->offApptoken = $officialaccountConfig['token'] ?? '';
    }
    public function index()
    {

        $data = input();
        Log::info('$data' . json_encode($data,320));
        if(isset($data['code'])){
            $config = [
                'app_id' => $this->offAppId,
                'secret' => $this->offAppSecret,
                'aes_key' => $this->offEncodingAESKey,
                'token' =>  $this->offApptoken,
            ];
            $app = new \EasyWeChat\OfficialAccount\Application($config);
            Log::info('12312312312' . json_encode($app,320));
            $oauth = $app->getOauth();
            $user = $oauth->userFromCode($data['code']);
            $user = $user->toArray();
            $encryptedData = $_POST['encryptedData'];
            $iv = $_POST['iv'];
            $sessionKey = $oauth->session_key;
            $decryptedData = openssl_decrypt(    base64_decode($encryptedData),    'AES-128-CBC',    base64_decode($sessionKey),    OPENSSL_RAW_DATA,    base64_decode($iv));
            $data = json_decode($decryptedData, true);
            $user['unionId'] = $data['unionId'];
            return json_encode(['code'=>0,'msg'=>'获取成功','data'=>$user]);
        }

        if (empty($data['signature']) || empty($data['timestamp']) || empty($data['nonce']) || empty($data['echostr'])) {
            return -1;
        }

        $signature = $data['signature'];
        $timestamp = $data['timestamp'];
        $nonce = $data['nonce'];
        $echostr = $data['echostr'];

        $token = 'huiminyoupin';

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {

            return $data['echostr'] ;
        } else {
            return -1;
        }
    }

// 配置参数$config = [    'app_id' => 'your-app-id',    'secret' => 'your-app-secret',    // ...];
//// 创建一个微信应用实例$app = Factory::officialAccount($config);
/// // 用户授权登录，获取用户的openid和session_key$auth = $app->oauth->user();
/// // 使用session_key解密获取用户的unionid
/// $encryptedData = $_POST['encryptedData'];
/// $iv = $_POST['iv'];
/// $sessionKey = $auth->session_key;
/// $decryptedData = openssl_decrypt(    base64_decode($encryptedData),    'AES-128-CBC',    base64_decode($sessionKey),    OPENSSL_RAW_DATA,    base64_decode($iv));
/// $data = json_decode($decryptedData, true);
/// $unionId = $data['unionId'];
}